<?php
namespace Home\Controller;
use Home\Controller\HomeController;

class CommentController extends HomeController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    //评价晒单首页
    public function index(){
        $where['order_id'] = I('get.order_id');
        //无订单id跳转首页
        if($where['order_id'] == ""){
            $this->redirect('Index/index');
        }

        //订单id是否合法
        $order = $this->order_state($where['order_id']);

        if(!$order){
            $this->redirect('Index/index');
        }
        $this->assign('order',$order);

        //查询订单商品
        $order_goods = M('order_goods')->where(array('order_id'=>$where['order_id']))->select();
        $this->assign('order_goods',$order_goods);
//        p($order);die;

        $this->display();
    }

    //评价
    public function comment()
    {
        $arr = I('post.');
//        //无订单id跳转首页
        if($arr['rec_id'] == ""){
            $this->redirect('Index/index');
        }

//        订单id是否合法
        $order = $this->order_state($arr['rec_id']);
        if(!$order){
            $this->redirect('Index/index');
        }

        $goods_comment = M("goods_comment");

        foreach($arr['goods_id'] as $k=>$v){
            $data['user_id'] = session('user_id');
            $data['rec_id'] =$arr['rec_id'];

            $data['fahuo'] = $arr['fahuo'] ? $arr['fahuo'] : 5;
            $data['fuwu'] = $arr['fuwu'] ? $arr['fuwu'] : 5;
            $data['wuliu'] = $arr['wuliu'] ? $arr['wuliu'] : 5;

            $data['goods_id'] = $v;
            $data['goods_name'] = $arr['goods_name'][$k];
            $a['goods_name'][] = $data['goods_name'];
            $a['goods_id'][] = $v;

            $goods_img = 'goods_img'.$v;
            if($arr[$goods_img]){
                $data['goods_img'] = serialize($arr[$goods_img]);
            }

            $user_name = 'user_name'.$v;
            $data['user_name'] = session('user_auth')['user_name'];
            $data['is_anonymous'] = $arr[$user_name] ? 1 : 0;
            $data['headimgurl'] = session('user_auth')['user_avatar'];
            $data['add_time'] = time();
            $data['comment'] = $arr['comment'][$k] ? $arr['comment'][$k] : '这位用户比较懒，没有留下任何信息';
            $miaoshu = 'miaoshu'.$v;
            $data['miaoshu'] = $arr[$miaoshu] ? $arr[$miaoshu] : 5;
            $data['is_show'] = 0;
            $re = $goods_comment->data($data)->add();
            if($re){
                //赠送积分
                if(!empty($data['goods_img'])){
                    M('users')->where('user_id='.  session('user_id'))->setInc('integral',C('integral_pcomment_state'));
                }else{
                    M('users')->where('user_id='.  session('user_id'))->setInc('integral',C('integral_tcomment_state'));
                }
            }
        }
        $this->assign('a',$a);

        M('order')->where(array('order_id'=>$data['rec_id']))->save(array('evaluation_state'=>1));

        //选项卡商品
        $where['goods_sale'] = 1;
        $where['goods_state'] = 1;
        $wxdgoods = M('goods')->where($where)->limit(10)->select();
        $this->assign('wxdgoods',$wxdgoods);

        $this->display();
    }


    protected function order_state($order_id){
        $where['order_id'] = $order_id;
        //查不到此id跳转至首页 状态必须为 已完成 未退款 未评价 该用户的
        $where['evaluation_state'] = 0;
        $where['order_state'] = 40;
        $where['refund_state'] = 0;
        $where['user_id'] = session('user_id');
        $order = M('order')->where($where)->field('store_name,store_id,order_id')->find();
        return $order;
    }

    //点赞
    public function zan(){
        $p = I('post.');
        //用户出去登陆状态才可执行
        if(session('user_id')){
            //判断是否有这条评价
            $where['id'] = $p['comment_id'];
            $re = M('goods_comment')->where($where['id'])->count();
            if($re){
                //1：点赞   0：取消点赞
                if($p['pan']){
                    $data['goods_id'] = $p['goods_id'];
                    $data['comment_id'] = $p['comment_id'];
                    $data['user_id'] = session('user_id');
                    M('goods_comment_zan')->data($data)->add();
                    M('goods_comment')->where(array('id'=>$data['comment_id']))->setInc('thumbs_up',1);
                }else{
                    $data['goods_id'] = $p['goods_id'];
                    $data['comment_id'] = $p['comment_id'];
                    $data['user_id'] = session('user_id');
                    M('goods_comment_zan')->where($data)->delete();
                    M('goods_comment')->where(array('id'=>$data['comment_id']))->setDec('thumbs_up',1);
                }
                $this->ajaxReturn(array('true'));
            }
        }
    }

}